package icegps.com.pingdj.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.ArrayList;

import butterknife.BindView;
import icegps.com.pingdj.R;
import icegps.com.pingdj.activity.settingactivity.SystemSettingActivity;
import icegps.com.pingdj.adapter.MenuAdapter;
import icegps.com.pingdj.constant.Constant;
import icegps.com.pingdj.data.ble.BleHelper;
import icegps.com.pingdj.data.ble.WriteHelper;
import icegps.com.pingdj.constant.MenuCons;
import icegps.com.pingdj.utils.Utils;

import static icegps.com.pingdj.constant.ActionKey.MEASURE_AVE_HEIGHT;
import static icegps.com.pingdj.constant.ActionKey.TITLE;
import static icegps.com.pingdj.constant.ActionKey.LOAD_TOKEN;

public class MainActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private ArrayList<String> menus = new ArrayList<>();
    private MenuAdapter menuAdapter;
    private RxPermissions rxPermissions;

    @Override
    protected String setTitle() {
        return getString(R.string.main_menu);
    }

    @Override
    protected void init() {
        rxPermissions = new RxPermissions(activity);
        initData();
        initRecycleView();
        setListener();
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    private void setListener() {
        menuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String s = menus.get(position);
                if (s.equals(MenuCons.MENU_1)) {
                    if (status == 0) {
                            rxPermissions.request(
                                    Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                                    .subscribe(grand -> {
                                        if (grand) {
                                            Intent intent = new Intent(activity, MapActivity.class);
                                            intent.putExtra(TITLE, getString(R.string.land_measure));
                                            startActivity(intent);
                                        } else {
                                            Utils.showToast("权限被拒绝,无法正常工作");
                                        }
                                    });

                    } else {
                        Utils.showToast(getString(R.string.location_status_do_no_user));
                    }
                }
                if (s.equals(MenuCons.MENU_2)) {
                    if (status == 0) {
                        rxPermissions.request(
                                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                                .subscribe(grand -> {
                                    if (grand) {
                                        Intent intent = new Intent(activity, PlaneActivity.class);
                                        intent.putExtra(MEASURE_AVE_HEIGHT, Constant.measureAveHeight);
                                        startActivity(intent);
                                    } else {
                                        Utils.showToast("权限被拒绝,无法正常工作");
                                    }
                                });
                    } else {
                        Utils.showToast(getString(R.string.location_status_do_no_user));
                    }

                }
                if (s.equals(MenuCons.MENU_3)) {
                    if (status == 4) {
                        rxPermissions.request(
                                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
                                .subscribe(grand -> {
                                    if (grand) {
                                        Intent intent = new Intent(activity, SlopeActivity1.class);
                                        intent.putExtra(MEASURE_AVE_HEIGHT, Constant.measureAveHeight);
                                        startActivity(intent);
                                    } else {
                                        Utils.showToast("权限被拒绝,无法正常工作");
                                    }
                                });
                    } else {
                        Utils.showToast(getString(R.string.location_status_do_no_user));
                    }
                }
                if (s.equals(MenuCons.MENU_4)) {
                    startActivity(LocationInfoActivity.class);
                }
                if (s.equals(MenuCons.MENU_5)) {
                    startActivity(SatelliteSignalActivity.class);
                }
                if (s.equals(MenuCons.MENU_6)) {
                    startActivity(MeasureDataActivity.class);
                }
                if (s.equals(MenuCons.MENU_7)) {
                    startActivity(SystemSettingActivity.class);
                }
                if (s.equals(MenuCons.MENU_8)) {
                    if (status == 4) {
                        startActivity(ContinueWorkActivity.class);
                    }else {
                        Utils.showToast(getString(R.string.location_status_do_no_user));
                    }
                }
                if (s.equals(MenuCons.MENU_9)) {
                    startActivity(BleActivity.class);
                }
            }
        });
    }

    private void initData() {
        menus.add(MenuCons.MENU_1);
        menus.add(MenuCons.MENU_2);
        menus.add(MenuCons.MENU_3);
        menus.add(MenuCons.MENU_4);
        menus.add(MenuCons.MENU_5);
        menus.add(MenuCons.MENU_6);
        menus.add(MenuCons.MENU_7);
        menus.add(MenuCons.MENU_8);
        menus.add(MenuCons.MENU_9);
    }

    private void initRecycleView() {
        menuAdapter = new MenuAdapter(R.layout.item_menu, menus);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerView.setAdapter(menuAdapter);
    }


    @Override
    protected void onResume() {
        super.onResume();
        WriteHelper.write(WriteHelper.CONNECT);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleHelper.getInstance().disconnectAllDevice();
    }

    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage(getString(R.string.quit) + "?")
                .setNegativeButton(R.string.cancel, null)
                .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).show();
    }
}
